// pages/feedback/feedback.js
Page({
  data: {
    feedback: '', // 意见内容
    contact: '', // 联系方式
    feedbackTypes: ['功能建议', '页面布局', '使用问题', '其他'],
    selectedType: '功能建议', // 默认选中的反馈类型
    maxLength: 500, // 最大输入长度
    currentLength: 0, // 当前输入长度
    feedbackContent: '',
    contactInfo: '',
    feedbackType: 'suggestion',
    contentVisible: false // 默认设置为不可见
  },


  checkContentVisibility: function() {
    const app = getApp();
    const isVisible = app.checkContentVisibility();
    this.setData({
      contentVisible: isVisible
    });
    return isVisible;
  },

  onLoad: function (options) {
    this.checkContentVisibility();
    
    if (!this.data.contentVisible) {
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
      }, 100);
      return;
    }
  },

  // 改变反馈类型
  bindTypeChange: function(e) {
    this.setData({
      selectedType: this.data.feedbackTypes[e.detail.value]
    });
  },

  // 输入意见内容
  inputFeedback: function(e) {
    const value = e.detail.value;
    this.setData({
      feedback: value,
      currentLength: value.length
    });
  },

  // 输入联系方式
  inputContact: function(e) {
    this.setData({
      contact: e.detail.value
    });
  },

  // 提交反馈
  submitFeedback: function() {
    if (!this.checkContentVisibility()) {
      return;
    }

    const { content, contact, type } = this.data;
    
    if (!content.trim()) {
      return;
    }

    const feedbackData = {
      content,
      contact,
      type,
      timestamp: new Date().getTime()
    };

    const feedbacks = wx.getStorageSync('feedbacks') || [];
    feedbacks.push(feedbackData);
    wx.setStorageSync('feedbacks', feedbacks);

    this.setData({
      content: '',
      contact: '',
      type: 'suggestion'
    });
  },

  // 重置表单
  resetForm: function() {
    this.setData({
      feedback: '',
      contact: '',
      selectedType: '功能建议',
      currentLength: 0
    });
  }
}) 